package com.sibrary.booxoo.download;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import com.sibrary.booxoo.BooxooDownloadActivity;
import com.sibrary.utils.Constants;
import com.sibrary.utils.DomUtils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.webkit.DownloadListener;
import android.widget.Toast;

public class BooxooDownloadController {

	private int index; // 当前正在下载个数

	public Queue<BooxooDownloadItem> mDownloadQueues; // 等待下载队列
	public List<BooxooDownloadItem> mDownloads; // 下载管理列表

	// @SuppressLint("HandlerLeak")
	public Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Constants.MESSAGE_DOWNLOAD_SUCCESSFUL:
			case Constants.MESSAGE_DOWNLOAD_FAILED: {
				notifyStop();
				notifyStart();
				break;
			}
			case Constants.MESSAGE_DOWNLOAD_START:
			case Constants.MESSAGE_DOWNLOAD_RUNNING:
			case Constants.MESSAGE_DOWNLOAD_FINISH:
			case Constants.MESSAGE_DOWNLOAD_CANCEL: {
				BooxooDownloadEventController.getInstance().trigger(msg.what,
						msg.obj);
				break;
			}
			case Constants.MESSAGE_DOWNLOAD_NO_SPACE: {
				Toast.makeText(Constants.mContext,
						"SDCard does not enough space.", Toast.LENGTH_SHORT)
						.show();
				break;
			}
			default:
				break;
			}
		}
	};

	/**
	 * Holder for singleton implementation.
	 */
	private static class BooxooDownloadControllerHolder {
		private static final BooxooDownloadController INSTANCE = new BooxooDownloadController();
	}

	/**
	 * Get the unique instance of the Controller.
	 * 
	 * @return The instance of the Controller
	 */
	public static BooxooDownloadController getInstance() {
		return BooxooDownloadControllerHolder.INSTANCE;
	}

	/**
	 * 私有构造函数
	 */
	private BooxooDownloadController() {
		// TODO Auto-generated constructor stub
		mDownloads = new ArrayList<BooxooDownloadItem>();
		mDownloadQueues = new LinkedList<BooxooDownloadItem>();
		index = 0;
	}

	/**
	 * 通知下载
	 */
	public void notifyStart() {
		if (!mDownloadQueues.isEmpty() && index < Constants.TOTAL) {
			// 执行下载
			++index;
			BooxooDownloadItem item = null;
			while (!mDownloadQueues.isEmpty()) {
				item = mDownloadQueues.poll();
				if (!item.getCancel()) {
					break;
				}
			}

			BooxooDownloadRunnable mr = new BooxooDownloadRunnable(item);
			item.mRunnable = mr;
			new Thread(mr).start();
		}
	}

	/**
	 * 通知下载完成
	 */
	public void notifyStop() {
		--index;
		if (index < 0) {
			index = 0;
		}
	}

	/**
	 * 添加下载项
	 */
	public void addItem(BooxooDownloadItem item) {
		if (item != null && !mDownloadQueues.contains(item)) {
			mDownloadQueues.add(item);
		}

		if (item != null && !mDownloads.contains(item)) {
			mDownloads.add(item);
		}
	}

	/**
	 * 添加到xml文件中
	 */
	public void saveItem(BooxooDownloadItem item) {
		if (item != null) {
			if (!DomUtils.isExists()) {
				DomUtils.createXml(item);
			} else {
				DomUtils.insertItemInXml(item);
			}
		}
	}

	/**
	 * 修改xml文件
	 */
	public void alterItem(BooxooDownloadItem item) {
		if (item != null && DomUtils.isExists()) {
			DomUtils.alterItemInXml(item);
		}
	}

	/**
	 * 测试url是否在mDownloads中存在
	 */
	public BooxooDownloadItem getItemOfSameUrlInDownloads(String url) {
		for (int i = 0; i < mDownloads.size(); ++i) {
			BooxooDownloadItem tmp = mDownloads.get(i);
			if (url.equals(tmp.getUrl())) {
				return tmp;
			}
		}
		return null;
	}
}
